﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Catalogo.Portable.ViewModel;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Windows.Networking.Proximity;
using System.Text;
using System.Runtime.InteropServices.WindowsRuntime;
using Catalogo.Portable.ViewModel.Messages;

namespace Catalogo.WindowsPhone.Views
{
    public partial class ItemPage : PhoneApplicationPage
    {
        private ProximityDevice _DispositivoNFC;
        private long _IdMensagemNFC;
        public ItemPageViewModel ViewModel
        {
            get
            {
                return DataContext as ItemPageViewModel;
            }
        }

        public ItemPage()
        {
            InitializeComponent();
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {           
            var idItem = NavegarParaPaginaMessageHandler.ObterParametro(e.Uri);
            if (ViewModel == null)
            {
                DataContext = new ItemPageViewModel();
                ViewModel.Messenger.Subscribe<NavegarParaPagina>(NavegarParaPaginaMessageHandler.NavegarParaPagina);
                await ViewModel.LoadAsync(idItem);
                Panorama.Background = new ImageBrush() { ImageSource = new BitmapImage(new Uri(ViewModel.Empresa.ImageUrl)), Stretch = Stretch.UniformToFill };
            }
        }

        private void ShareButton_Click(object sender, EventArgs e)
        {
            if (ViewModel.Item != null)
            {
                var shareLinkTask = new ShareLinkTask()
                {
                    Title = ViewModel.ObterTituloParaCompartilhar(),
                    LinkUri = ViewModel.ObterUriParaCompartilhar(),
                    Message = ViewModel.Item.Descricao,
                };
                shareLinkTask.Show();
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            CancelarGravacaoTag();
        }

        private void WriteTagButton_Click(object sender, EventArgs e)
        {
            if (ViewModel.Item != null)
            {
                if (ProximityDevice.GetDefault() != null)
                {
                    CancelarGravacaoTag();

                    _DispositivoNFC = ProximityDevice.GetDefault();
                    var buffer = Encoding.UTF8.GetBytes(ViewModel.Item.Id);
                    _IdMensagemNFC = _DispositivoNFC.PublishBinaryMessage("Windows:WriteTag.MySubType", buffer.AsBuffer(), NFCGravado);
                }
                else
                {
                    MessageBox.Show("NFC não encontrado!");
                }
            }
        }

        private void CancelarGravacaoTag()
        {
            if (_IdMensagemNFC > 0)
            {
                _DispositivoNFC.StopPublishingMessage(_IdMensagemNFC);
            }
        }

        private void NFCGravado(ProximityDevice sender, long messageId)
        {
            _DispositivoNFC.StopPublishingMessage(_IdMensagemNFC);
            _IdMensagemNFC = -1;
            Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show("Tag gravada!");
            });
        }
    }
}